<?php

namespace app\netdisk\controller;

use app\BaseController;

use app\netdisk\model\{
    ShareInfo as ShareModel,
};


class Admin extends BaseController
{

    ######################################################################################################################
    # 数据管理接口
    ######################################################################################################################

    // 显示资源列表
    public function index()
    {
        $where = $this->request->only([
            'page' => 1,
            'limit' => 10,
            'share_name' => '', // 文件名
            'share_code' => '', // 链接码
            'start_time' => '', // 开始时间
            'end_time' => '', // 结束时间
        ], 'get');

        return $this->success(ShareModel::getAll($where, []));
    }

    // 保存新建的资源
    public function save()
    {
        $data = $this->request->param();

        if ($data['share_link'] != '')
            $data['share_code'] = substr($data['share_link'], -6);

        return $this->success(ShareModel::create($data));
    }

    // 保存更新的资源
    public function update($id)
    {
        $data = $this->request->param();

        if ($data['share_link'] != '')
            $data['share_code'] = substr($data['share_link'], -6);

        return $this->success(ShareModel::update($data));
    }

    // 删除指定资源
    public function delete($id)
    {
        return $this->success(ShareModel::destroy($id));
    }
}
